package com.app.manage.config;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.PostConstruct;
import java.util.Map;

@Component
public class RequestMappingExtractor {

    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @PostConstruct
    public void printAllMappings() {
        Map<RequestMappingInfo, HandlerMethod> map = handlerMapping.getHandlerMethods();
        map.forEach((requestMappingInfo, handlerMethod) -> {
            if (requestMappingInfo.getMethodsCondition().getMethods().contains(RequestMethod.GET) ||
                    requestMappingInfo.getMethodsCondition().getMethods().contains(RequestMethod.POST)) {
                System.err.println(requestMappingInfo + " -> " + handlerMethod);
            }
        });
    }
}